package com.wyh.book.controller;

import com.wyh.book.enums.StringConstant;
import com.wyh.book.model.UserInfo;
import com.wyh.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:登录校验
 * User: 王圆豪
 * Date: 2025-07-02
 * Time: 17:26
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        if (!StringUtils.hasText(name) ||
                !StringUtils.hasText(password)){
            return false;
        }
        UserInfo userInfo = userService.selectPassword(name);
        if (userInfo == null) return false;

        if (password.equals(userInfo.getPassword())){
            session.setAttribute(StringConstant.SESSION_USER_KEY, userInfo);
            return true;
        }

        return false;
    }
}
